/*
     Copyright (C) 2011 Benny Mackney

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "Vec2D.h"

Vec2D::Vec2D(){
    x = 0;
    y = 0;
}

Vec2D::Vec2D(double newX, double newY)
{
    x = newX;
    y = newY;
}

Vec2D::~Vec2D()
{
    //dtor
}

double Vec2D::length() const{
    return sqrt(pow(x,2)+pow(y,2));
}

double Vec2D::length2() const{
    return (pow(x,2)+pow(y,2));
}

void Vec2D::set(double newX, double newY){
    x = newX;
    y = newY;
}

void Vec2D::set_x(double newX){
    x = newX;
}

void Vec2D::set_y(double newY){
    y = newY;
}

double Vec2D::angle() const {
    double angle = atan2(x,y);
    if (angle < 0) angle += BSC_TAU;
    return angle;
}


Vec2D & Vec2D::operator+=(const Vec2D &other){
    x += other.x;
    y += other.y;
    return *this;
}

Vec2D & Vec2D::operator-=(const Vec2D &other){
    x -= other.x;
    y -= other.y;
    return *this;
}

Vec2D Vec2D::operator+(const Vec2D &rhs){
    return Vec2D(x+rhs.get_x(),y+rhs.get_y());
}

Vec2D Vec2D::operator-(const Vec2D &rhs){
    return Vec2D(x-rhs.get_x(),y-rhs.get_y());
}

double Vec2D::get_x() const{
    return x;
}
double Vec2D::get_y() const{
    return y;
}

void Vec2D::scale(double multiplier){
    x = x*multiplier;
    y = y*multiplier;
}
void Vec2D::scale_to(double endLength){
    double startLength = length();
    if(startLength != 0){
        double multiplier = endLength/startLength;
        scale(multiplier);
    }
}

void Vec2D::rotate(double angleShift){
    double newAngle = angle()+angleShift;
    double oldLength = length();
    x = sin(newAngle);
    y = cos(newAngle);
    scale_to(oldLength);
}

void Vec2D::rotate_around(const Vec2D &axis,double angleShift){
    (*this) -= axis;
    rotate(angleShift);
    (*this) += axis;
}
